const {
  mrjAsyncBaseReq, //  基本请求
  BaseRequestModel, // 基本请求对象
  mrjAsyncUploadReq
} = require('./mrhhttp')
const api_upload_file = "/upload"
const api_get_session_key = "/auth/wechat/session/key"// 获取sessionKey
const api_wx_decode = "/auth/wechat/decode"// 通用解码接口

const api_login_with_unionId = "/auth/member/unionid/login"
const api_regist_width_unionId = "/auth/member/unionid/register"
const api_bind_with_unionId = "/auth/wechat/unionid/bind"
const api_check_unionId_status = "/auth/member/unionid/check";//,针对C端账号的检查是否注册了
const api_load_sms_captcha = "/captcha/sms"
const api_check_sms_captcha = "/captcha/sms/check"
const CommonHttp = {
  /**
   * 微信登录获取code
   * @param {参数} param 
   * @param {*} method 
   */
  mrjAsyncWXLogin: (param = {}, method = 'POST') => {
    return new Promise((reslove, reject) => {
      var start = new Date().getTime();
      wx.login({
        success: (res) => {
          var end = new Date().getTime();
          console.log('wx.login()' + "耗时:" + (end - start) + 'ms');
          reslove && reslove(res)
        },
        fail: (err) => {
          var end = new Date().getTime();
          console.log('wx.login()' + "耗时:" + (end - start) + 'ms');
          reject && reject(err);
        }
      })
    })
  },
  asyncUploadImage:async (param)=>{
    const res = await mrjAsyncUploadReq({
      ...BaseRequestModel,
      url: `${BaseRequestModel.baseUrl}${api_upload_file}`,
      param:param,
      method:'POST'
    });
    if(typeof res.data === 'string'){
      return JSON.parse(res.data)
    }else{
      return res.data
    }
  },
  asyncLoginWithUnionId:async (param)=>{

    const res = await mrjAsyncBaseReq({
      ...BaseRequestModel,
      url: `${BaseRequestModel.baseUrl}${api_login_with_unionId}`,
      param: param
    });
    return res.data

  },

  asyncRegistWithUnionId:async (param)=>{
    const res = await mrjAsyncBaseReq({
      ...BaseRequestModel,
      url: `${BaseRequestModel.baseUrl}${api_regist_width_unionId}`,
      param: param
    });
    return res.data

  },
  asyncBindAccountAndUnionId:async param =>{
    const res = await mrjAsyncBaseReq({
      ...BaseRequestModel,
      url: `${BaseRequestModel.baseUrl}${api_bind_with_unionId}`,
      param: param
    });
    return res.data
  },
  checkUnionIdStatus:async param=>{
    const res = await mrjAsyncBaseReq({
      ...BaseRequestModel,
      url: `${BaseRequestModel.baseUrl}${api_check_unionId_status}`,
      param: param
    });
    return res.data
    
  },
  loadSessionKey:async param=>{
    const res = await mrjAsyncBaseReq({
      ...BaseRequestModel,
      url: `${BaseRequestModel.baseUrl}${api_get_session_key}`,
      param: param
    });
    return res.data
  },
  commonWXDecode:async param=>{
    const res = await mrjAsyncBaseReq({
      ...BaseRequestModel,
      url: `${BaseRequestModel.baseUrl}${api_wx_decode}`,
      param: param
    });
    return res.data
  },

  getSMSCheckcode:async param =>{
    const res = await mrjAsyncBaseReq({
      ...BaseRequestModel,
      url: `${BaseRequestModel.baseUrl}${api_load_sms_captcha}`,
      param: param
    });
    return res.data
  },
  checkCheckcodeIsOk:async param =>{
    const res = await mrjAsyncBaseReq({
      ...BaseRequestModel,
      url: `${BaseRequestModel.baseUrl}${api_check_sms_captcha}`,
      param: param
    });
    return res.data
  }
}
module.exports = CommonHttp;